package com.qf.fmall.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.index.entity.Product;
import com.qf.fmall.index.entity.ProductImg;
import com.qf.fmall.index.mapper.ProductImgMapper;
import com.qf.fmall.index.mapper.ProductMapper;
import com.qf.fmall.index.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weijy
 * @since 2024-01-16
 */
@Service
@Slf4j
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImgMapper productImgMapper;
    @Override
    public List<Product> getListRecommends() {

        List<Product> productList =
                productMapper.selectList(new QueryWrapper<Product>()
                .orderByDesc("create_time")
                .last("limit 3"));

        if (productList == null ){
            return null;
        }

        for (Product product : productList) {
            List<ProductImg> secondList =
                    productImgMapper.selectList(new QueryWrapper<ProductImg>()
                    .eq("product_id", product.getProductId()));

            if (secondList != null){
                product.setImgs(secondList);
            }

        }

        log.info("输出获取的列表: productList={}",productList);

        return productList;
    }

    @Override
    public Object getProductById(Integer productId) {
        List<Product> list = list(new QueryWrapper<Product>().eq("product_id",productId));

        return list;
    }
}
